import { useCallback, useEffect, useState } from 'react'
import { useLocation } from 'react-router-dom'
// import {} from '../service'
import { Form } from 'antd'
import moment from 'moment'
import { isNullArray, isNullData } from '@/utils'
import { CheckHooks, VariableList } from '../data'
import { getWord, WordList } from '@/services/common'
import { TableInfo } from '@/pages/ReportDetails/data'
import { getDataTaskDetail, getInjuryInformation } from '../service'
import { IPressure, IVariableSort } from '@/@types/common'

interface ParamsRout extends TableInfo {
  task: string[]
}
export default (): CheckHooks => {
  const parasm = useLocation()
  const { hospitalId, hospitalName, task: paramstask } = parasm.state as ParamsRout
  console.log('parasm.state🚀🚀🚀🚀', parasm.state)
  const [formObj] = Form.useForm()
  // 当前选中任务
  const [task, setTask] = useState<string[]>(paramstask)
  // 病区
  const [wordList, setWordList] = useState<WordList[]>([])
  // 存放当前选中病区
  const [selectWord, setSelectWord] = useState<number | undefined>()
  // 变量下拉框
  const [variable, setVariable] = useState<VariableList[]>([
    {
      value: 1,
      label: '变量数据',
    },
    {
      value: 2,
      label: '指标数据',
    },
  ])
  // 存放标量指标数据
  const [varableTableList, setVariableList] = useState<IVariableSort[]>([])
  // 控制modal
  const [visible, setVisible] = useState<boolean>(false)
  // 事件数据
  const [eventList, setEventList] = useState<IPressure[]>([])
  // 变量loading
  const [varableLoading, setVariableLoading] = useState<boolean>(false)
  // 保存当前查看的
  const [lookItem, setLookItem] = useState<IPressure>()

  // 存放变量指标下拉框
  const [tag, setTag] = useState<number>(1)

  // 获取地区
  const getWords = useCallback(async () => {
    const res = await getWord({
      hospitalId,
      dataReportQuarter: task[1] ? task[1] : '',
      dataReportYear: task[0] ? task[0] : '',
    })
    if (isNullData(res) && res.data && isNullArray(res.data)) {
      setWordList(res.data)
      setSelectWord(res.data[0].dataId)
    } else {
      setWordList([])
    }
  }, [])

  // 任务下拉框变化时触发
  const handleTaskChange = useCallback((e: string[]) => {
    setTask(e)
  }, [])

  // 病区发生变化时触发
  const handleWordChange = useCallback((e: number) => {
    setSelectWord(e)
  }, [])

  // 查询变量信息
  const getVarableList = async () => {
    setVariableLoading(true)
    const res = await getDataTaskDetail({
      dataId: selectWord!,
    })
    if (isNullData(res) && res.data && isNullArray(res.data)) {
      setVariableList(res.data.sortPlatformVos)
    } else {
      setVariableList([])
    }
    setVariableLoading(false)
    console.log('变量数据🚀🚀🚀🚀', res)
  }

  const getInjuryInfo = async () => {
    const res = await getInjuryInformation({
      dataId: selectWord!,
    })
    if (isNullData(res) && res.data && isNullArray(res.data)) {
      setEventList(res.data)
    } else {
      setEventList([])
    }
    console.log('事件内容🚀🚀🚀🚀', res)
  }

  // 变量下拉框
  const handleVaribleChange = useCallback((e: number) => {
    setTag(e)
  }, [])

  // 查看
  const handlePreview = (list: IPressure) => {
    formObj.setFieldsValue({
      ...list,
      admissionTime: moment(list.admissionTime),
      happenDate: moment(list.happenDate),
      happenArea: list.happenAreaId.split('/'),
    })
    setLookItem(list)
    setVisible(true)
  }

  // modal确定
  const handleOk = (isHCancel: boolean) => {
    console.log('确定🚀🚀🚀🚀')
  }
  // 取消关闭回调
  const handleCancel = () => {
    console.log('取消🚀🚀🚀🚀')
    formObj.resetFields()
    setVisible(false)
  }

  useEffect(() => {
    getWords()
  }, [])

  useEffect(() => {
    if (selectWord) {
      getVarableList()
      getInjuryInfo()
    }
  }, [selectWord])

  return {
    task,
    handleTaskChange,
    wordList,
    handleWordChange,
    variable,
    varableTableList,
    eventList,

    handleVaribleChange,
    tag,
    selectWord,
    hospitalName,
    varableLoading,
    handlePreview,
    paramstask,
    IndicatorsModal: {
      formObj,
      visible,
      handleOk,
      handleCancel,
      lookItem,
      hospitalId,
    },
  }
}
